package com.tianya.common.util;

import java.util.HashMap;
import java.util.Map;

public class MessageInfo {

	private String title;
	private String messageType;
	private String messageText;
	private String url;
	private Map<String,MessageButton> buttons;
	
	public MessageInfo()
	{
		buttons = new HashMap<String,MessageButton>();
	}
	
	public MessageInfo(String text)
	{
		buttons = new HashMap<String,MessageButton>();
		setMessageType(MessageType.Information.toString());
		setMessageText(text);
	}
	
	public MessageInfo(MessageType t,String text)
	{
		buttons = new HashMap<String,MessageButton>();
		setMessageType(t.toString());
		setMessageText(text);
	}
	public MessageInfo(MessageType t,String text,String url)
	{
		buttons = new HashMap<String,MessageButton>();
		setMessageType(t.toString());
		setMessageText(text);
		setUrl(url);
	}
	public void addButton(String text,String url)
	{
		addButton(new MessageButton(String.valueOf(buttons.size()),text,url));
	}
	
	public void addButton(MessageButton button)
	{
		if(buttons == null)
		{
			buttons = new HashMap<String,MessageButton>();
		}
		buttons.put(button.getId(), button);
	}
	public String getButtonsString()
	{
		if(buttons == null || buttons.size() <=0)
		{
			return "";
		}
		//,btn:[['是','yes'],['否','no'],['取消','cancel']]
		String strButtons = ",btn:[";
		boolean first = true;
		for(MessageButton btn:buttons.values())
		{
			if(first == false)
			{
				strButtons +=",";
			}
			else
			{
				first = false;
			}
			
			strButtons += "['" + btn.getText() +"','" + btn.getId() + "']";
		}
		return strButtons + "]";
	}
	
	public String getResultScript(String tag,String tagMethod)
	{
		String strResult = "";
		for(MessageButton btn:buttons.values())
		{
			strResult += "if("+tag +"=='"+btn.getId()+"'){"+tagMethod+"('"+btn.getTagurl()+"');}";
		}
		return strResult;
	}
	
	
	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}
	public Map<String, MessageButton> getButtons() {
		return buttons;
	}
	public void setButtons(Map<String, MessageButton> buttons) {
		this.buttons = buttons;
	}


	public String getMessageType() {
		return messageType;
	}


	public void setMessageType(String messageType) {
		this.messageType = messageType;
	}


	public String getMessageText() {
		return messageText;
	}


	public void setMessageText(String messageText) {
		this.messageText = messageText;
	}
	public String getTitle() {
		if(title == null)
		{
			title = "";
		}
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
}
